<html>
<head><meta charset="utf-8"><title>How to return String as &amp;str from iterator · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html">How to return String as &amp;str from iterator</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210387734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210387734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210387734">(Sep 17 2020 at 13:18)</a>:</h4>
<p>Hello, I'm sorry if this is the wrong place to ask. I have a code block as:</p>
<div class="codehilite"><pre><span></span><code><span class="n">possible_anagrams</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">];</span><span class="w"></span>
<span class="n">HashSet</span>::<span class="n">from_iter</span><span class="p">(</span><span class="n">possible_anagrams</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="c1">// middle steps omitted for clarity</span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">as_str</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="kt">str</span><span class="o">&gt;&gt;</span><span class="p">())</span><span class="w"></span>
</code></pre></div>


<p>compiler is giving an error, and I don't understand how to use lifetimes annotations to handle it correctly.</p>
<div class="codehilite"><pre><span></span><code><span class="go">error[E0515]: cannot return value referencing local variable `p`</span>
<span class="go">  --&gt; src\lib.rs:18:23</span>
<span class="go">   |</span>
<span class="go">18 |         .map(|(p, _)| p.as_str())</span>
<span class="go">   |                       -^^^^^^^^^</span>
<span class="go">   |                       |</span>
<span class="go">   |                       returns a value referencing data owned by the current function</span>
<span class="go">   |                       `p` is borrowed here</span>
</code></pre></div>


<p>I would appreciate if anyone can point me in the right direction.</p>



<a name="210388063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210388063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210388063">(Sep 17 2020 at 13:20)</a>:</h4>
<p><code>p.to_lowercase()</code> returns a <code>String</code>. When you do <code>p.as_str()</code> there is nothing keeping that string alive after <code>.map()</code>. You should collection into <code>Vec&lt;String&gt;</code> instead.</p>



<a name="210388344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210388344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210388344">(Sep 17 2020 at 13:23)</a>:</h4>
<p>In this case it is not a problem of annotations, it is a problem of liftimes. <code>|(p, _)| p.as_str()</code> takes an Owned object in this case a <code>String</code>, it tries to return a barrow of it <code>&amp;str</code>, but then <code>drop</code>'s (frees) it. It is telling you that the <code>&amp;str</code>you are returning is pointing at freed memory so cant be used.</p>



<a name="210393912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210393912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210393912">(Sep 17 2020 at 14:02)</a>:</h4>
<p>so how would I have any chance to return a <code>HashSet&lt;&amp;'a str&gt;</code>. I'm trying to solve a exercise question and currently tests require me to return a <code>HashSet&lt;&amp;'a str&gt;</code> my solution was to use iterators on the source string so I could compare each to find an exact match</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">anagrams_for</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">word</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="n">possible_anagrams</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">HashSet</span><span class="o">&lt;&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">str</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">ordered</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">word</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">ordered</span><span class="p">.</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="p">().</span><span class="n">sort</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="n">HashSet</span>::<span class="n">from_iter</span><span class="p">(</span><span class="n">possible_anagrams</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">ne</span><span class="p">(</span><span class="n">word</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">word</span><span class="p">.</span><span class="n">len</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">.</span><span class="n">clone</span><span class="p">(),</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="p">()))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">_p</span><span class="p">,</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">p</span><span class="p">.</span><span class="n">sort</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">_p</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">_p</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;</span><span class="p">().</span><span class="n">eq</span><span class="p">(</span><span class="n">ordered</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">as_str</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="kt">str</span><span class="o">&gt;&gt;</span><span class="p">())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>would using loop version permit me to? Apperantly I cannot even clone to get out of this situation? Compiler warns about late lifetime binding.</p>



<a name="210394672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210394672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210394672">(Sep 17 2020 at 14:07)</a>:</h4>
<p>You'll have to return items from one of your arguments. in this case I think <code>possible_anagrams</code>.</p>



<a name="210395076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210395076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210395076">(Sep 17 2020 at 14:10)</a>:</h4>
<p>I thought I was iterating over possible_anagrams, does calling <code>p.to_lowecase()</code> makes it change from possible_anagram iterations to some temporary variable?</p>



<a name="210396604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210396604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210396604">(Sep 17 2020 at 14:20)</a>:</h4>
<p>Yes</p>



<a name="210396662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210396662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210396662">(Sep 17 2020 at 14:21)</a>:</h4>
<p><code>to_lowecase</code> returns an owned <code>String</code></p>



<a name="210396753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210396753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210396753">(Sep 17 2020 at 14:21)</a>:</h4>
<p><a href="https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase">https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase</a></p>



<a name="210398729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210398729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210398729">(Sep 17 2020 at 14:36)</a>:</h4>
<p>I have discarded all the map operations so I can iterate over possible_anagrams, but something is still not working right now when I try to it complains about </p>
<div class="codehilite"><pre><span></span><code><span class="w">    </span><span class="n">HashSet</span>::<span class="n">from_iter</span><span class="p">(</span><span class="n">possible_anagrams</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">ne</span><span class="p">(</span><span class="n">word</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">word</span><span class="p">.</span><span class="n">len</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">clone</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">t</span><span class="p">.</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="p">().</span><span class="n">sort</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">t</span><span class="p">.</span><span class="n">eq</span><span class="p">(</span><span class="n">ordered</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}))</span><span class="w"></span>
</code></pre></div>


<div class="codehilite"><pre><span></span><code><span class="go">   = note: expected struct `std::collections::HashSet&lt;&amp;&#39;a str, std::collections::hash_map::RandomState&gt;`</span>
<span class="go">              found struct `std::collections::HashSet&lt;&amp;&amp;str, _&gt;`</span>
</code></pre></div>


<p>why is it iterating over <code>&amp;&amp;str</code>how can I filter if I cannot iterate over possible_anagrams directly? As I understand right now I'm iterating over a reference to possible_anagrams array which composed with str references. As soon as I use clone or something compiler states that I need lifetime annotation at function decleration</p>
<div class="codehilite"><pre><span></span><code><span class="go">4  |   pub fn anagrams_for&lt;&#39;a&gt;(word: &amp;str, possible_anagrams: &amp;[&amp;str]) -&gt; HashSet&lt;&amp;&#39;a str&gt; {</span>
<span class="go">   |                                                          ------- help: add explicit lifetime `&#39;a` to the type of `possible_anagrams`: `&amp;[&amp;&#39;a str]`</span>
</code></pre></div>



<a name="210399017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210399017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210399017">(Sep 17 2020 at 14:38)</a>:</h4>
<p>try <code>into_iter</code> instead of <code>iter</code>, I think.</p>



<a name="210399351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210399351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210399351">(Sep 17 2020 at 14:40)</a>:</h4>
<p>No, sorry, that is not it. give me a min.</p>



<a name="210399992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210399992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210399992">(Sep 17 2020 at 14:44)</a>:</h4>
<p>Wtf. Is this from the code you were given? <code>pub fn anagrams_for&lt;'a&gt;(word: &amp;str, possible_anagrams: &amp;[&amp;str]) -&gt; HashSet&lt;&amp;'a str&gt; {</code></p>



<a name="210400116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210400116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210400116">(Sep 17 2020 at 14:46)</a>:</h4>
<p>The <code>'a</code> only appears in the return parameter. which is odd.</p>



<a name="210400199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210400199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210400199">(Sep 17 2020 at 14:46)</a>:</h4>
<p>Sorry I didn't see that earlier.</p>



<a name="210400346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210400346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210400346">(Sep 17 2020 at 14:47)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c90dcb7a114264ba7479ce1fd02309eb">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c90dcb7a114264ba7479ce1fd02309eb</a></p>



<a name="210400432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210400432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210400432">(Sep 17 2020 at 14:48)</a>:</h4>
<p>work, by adding the clone and the <code>'a</code> that the error recomended.</p>



<a name="210401456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210401456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210401456">(Sep 17 2020 at 14:55)</a>:</h4>
<p>If  you can use <code>pub fn anagrams_for&lt;'a&gt;(word: &amp;str, possible_anagrams: &amp;'a [&amp;'a str]) -&gt; HashSet&lt;&amp;'a str&gt; {</code>then you are good. If not then I want to know more about where this example came from!</p>



<a name="210402142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210402142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210402142">(Sep 17 2020 at 14:59)</a>:</h4>
<p>I have tried using it, It works but I failed some tests so I'm debugging if I made some logical mistakes. What suprises me is str in Rust is not <code>null char</code> based. cause when I try to see what possible_anagrams look like CLion reports three element side by side as a single value. Question is from <a href="https://exercism.io">https://exercism.io</a> Rust track - Anagram</p>



<a name="210402571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210402571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210402571">(Sep 17 2020 at 15:02)</a>:</h4>
<p>Yes, and <code>str</code> is a pointer and a length. So it can be a part of some other string.</p>



<a name="210403263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210403263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210403263">(Sep 17 2020 at 15:07)</a>:</h4>
<p>according to <a href="https://github.com/exercism/rust/tree/master/exercises/anagram">https://github.com/exercism/rust/tree/master/exercises/anagram</a> it does want case insensitve. I think we can change <code>let t = p.clone()</code> into <code>let t = p.to_lowercase();</code></p>



<a name="210405818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210405818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yakup Türkan <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210405818">(Sep 17 2020 at 15:24)</a>:</h4>
<p>thanks for the help. I have created a for loop version to wrap my mind. I'm bad at iterators. I have found that I was iterating on the clone of the str while I really need to iterate over <code>Vec&lt;char&gt;</code>. here is the final form.</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">anagrams_for</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">word</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="n">possible_anagrams</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="o">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">str</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nc">HashSet</span><span class="o">&lt;&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">str</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">ordered</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">ordered</span><span class="p">.</span><span class="n">sort</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="c1">// let mut set = HashSet::new();</span>
<span class="w">    </span><span class="c1">// for p in possible_anagrams {</span>
<span class="w">    </span><span class="c1">//     println!(&quot;p {}&quot;, p);</span>
<span class="w">    </span><span class="c1">//     if p.len() != word.len() { continue }</span>
<span class="w">    </span><span class="c1">//     else if p.to_lowercase().eq(&amp;word.to_lowercase()) { continue }</span>
<span class="w">    </span><span class="c1">//</span>
<span class="w">    </span><span class="c1">//     let mut t = p.to_lowercase().chars().collect::&lt;Vec&lt;char&gt;&gt;();</span>
<span class="w">    </span><span class="c1">//     t.sort();</span>
<span class="w">    </span><span class="c1">//     if t.eq(&amp;ordered) { set.insert(p.clone()); }</span>
<span class="w">    </span><span class="c1">// }</span>
<span class="w">    </span><span class="c1">//</span>
<span class="w">    </span><span class="c1">// set</span>

<span class="w">    </span><span class="n">HashSet</span>::<span class="n">from_iter</span><span class="p">(</span><span class="n">possible_anagrams</span><span class="p">.</span><span class="n">iter</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">clone</span><span class="p">()</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"></span>
<span class="w">            </span><span class="n">p</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">ne</span><span class="p">(</span><span class="o">&amp;</span><span class="n">word</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">()))</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">word</span><span class="p">.</span><span class="n">len</span><span class="p">())</span><span class="w"></span>
<span class="w">        </span><span class="p">.</span><span class="n">filter</span><span class="p">(</span><span class="o">|&amp;</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">to_lowercase</span><span class="p">().</span><span class="n">chars</span><span class="p">().</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">&gt;&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">t</span><span class="p">.</span><span class="n">sort</span><span class="p">();</span><span class="w"></span>
<span class="w">            </span><span class="n">t</span><span class="p">.</span><span class="n">eq</span><span class="p">(</span><span class="o">&amp;</span><span class="n">ordered</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}).</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="o">|</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">clone</span><span class="p">()))</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="210585879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20to%20return%20String%20as%20%26str%20from%20iterator/near/210585879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20to.20return.20String.20as.20.26str.20from.20iterator.html#210585879">(Sep 18 2020 at 23:39)</a>:</h4>
<p>There is also no point to using null terminators in the language if you always know the length of the string.<br>
The point of that question is to prompt the student to change the function signature slightly in order to be able to pass the tests, yes, it was the original author's theory about how to introduce lifetimes more specifically.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>